<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('VatCalculator')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */
 
 include_spip('functions/pdk_common');

 /** 
 *  VAT Calculator - calculates VAT of order products
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class VatCalculator {
    /**
     * AlohacartOrderModel
     */
    private $order_mdl;
    
    /**
     * EcataloguE Currency Model
     */
    private $currency_mdl;
    
    function __construct() {
        $this->order_mdl = pdk_load_class('AlohacartOrderModel','classes');
        $this->currency_mdl = pdk_load_class('CurrencyModel','classes');
    }
    
    /**
     *  Calculates VAT for all given order products,extentends $order_products array with new fields and returns totals array
     *  NB: If $order_products array is empty, it will be automatically selected for given id order
     *  @param int $id_order ID of order
     *  @param string $vat_type VAT calculation type. Can be: 
     *                          - 'add' (100+20 - original products prices are without VAT)
     *                          - 'deduct' (120-20 - original products prices are with VAT)
     *  @param float $vat_value VAT value in percents
     *  @param array $order_products Array of order product wich will be extended with new fields:
     *                               - price_without_vat
     *                               - total_price_without_vat
     *                               - total_price_with_vat
     *  @param array $totals Array of calculated totals, with such fields:
     *                       - total_with_vat
     *                       - total_without_vat
     *                       - total_vat
     */
    public function CalculateOrderProductsVAT($id_order, $vat_type, $vat_value, &$order_products = array(), &$totals = array()) {
        $order = $this->order_mdl->select_by_id($id_order);
        if (!$vat_value) $vat_value = 0;
        if (!$vat_type) $vat_type = 'add';
        $totals['total_with_vat'] = 0;
        $totals['total_without_vat'] = 0;
        $totals['total_vat'] = 0;
        
		foreach ($order_products as $key=>&$product) {
		    $product_price = round($product['price']*$order['currency_rate'], $this->currency_mdl->price_precision);
            if ($vat_type == 'add') {
                $vat_price = round($product_price*$vat_value/100, $this->currency_mdl->price_precision);
                $product['price_without_vat'] = $product_price;
            } else if ($vat_type == 'deduct') {
                $vat_price = round($product_price-$product_price/(1+$vat_value/100), $this->currency_mdl->price_precision);
                $product['price_without_vat'] = round($product_price - $vat_price, $this->currency_mdl->price_precision);                          
            }            
            $product['total_price_without_vat'] = round($product['price_without_vat']*$product['quantity'], $this->currency_mdl->price_precision);
            $product['total_price_with_vat'] = round(($vat_price+$product['price_without_vat'])*$product['quantity'], $this->currency_mdl->price_precision);
            
            $totals['total_with_vat'] += $product['total_price_with_vat'];
            $totals['total_without_vat'] += $product['total_price_without_vat'];
            $totals['total_vat'] += round($vat_price*$product['quantity'], $this->currency_mdl->price_precision);            
		}
    }
}
?>